package com.wjf.birdfish.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; 
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod; 

public class HttpClientUtils { 
	
         public static byte[] getBody(String url,Map parms){ 
				byte[] body=null; 
				//构造HttpClient的实例
			    HttpClient httpClient = new HttpClient(); 
				//创建GET方法的实例
			    PostMethod postMethod = new PostMethod(url); 
				//填入各个表单域的值
				NameValuePair[] data = new NameValuePair[parms.keySet().size()]; Iterator it = parms.entrySet().iterator(); 
				int i=0; 
				while (it.hasNext()) { 
					Map.Entry entry = (Map.Entry) it.next(); 
					Object key = entry.getKey(); 
					Object value = entry.getValue(); 
					data[i]=new NameValuePair(key.toString(),value.toString()); 
					i++;
				} 
                //将表单的值放入postMethod中				
				postMethod.setRequestBody(data); 
				int statusCode = 0;
				try{
					    //执行postMethod
						statusCode = httpClient.executeMethod(postMethod); // HttpClient对于要求接受后继服务的请求，象POST和PUT等不能自动处理转发
					    System.out.println("statusCode====="+statusCode);
						//301或者302
						if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY 
								|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { 
						//从头中取出转向的地址
							
						Header locationHeader = postMethod.getResponseHeader("location"); 
						String location = null; 
										   if (locationHeader != null) { 
												location = locationHeader.getValue(); 
												System.out .println("The page was redirected to:" + location); 
										   } else { 
											 System.err.println("Location field value is null."); 
										   }
						   }
						body = postMethod.getResponseBody(); 
					} catch (Exception e) { 
					  System.out.println("statusCode====="+statusCode);
				      e.printStackTrace(); 
				      
			        }
				return body;
		} 
         public static  String getStatusPing(String url){ 
        	    Map parms=new HashMap();
				//构造HttpClient的实例
			    HttpClient httpClient = new HttpClient(); 
				//创建GET方法的实例
			    PostMethod postMethod = new PostMethod(url); 
				//填入各个表单域的值
				NameValuePair[] data = new NameValuePair[parms.keySet().size()]; Iterator it = parms.entrySet().iterator(); 
				int i=0; 
				while (it.hasNext()) { 
					Map.Entry entry = (Map.Entry) it.next(); 
					Object key = entry.getKey(); 
					Object value = entry.getValue(); 
					data[i]=new NameValuePair(key.toString(),value.toString()); 
					i++;
				} 
             //将表单的值放入postMethod中				
				postMethod.setRequestBody(data); 
				int statusCode = 0;
				try{
					//执行postMethod
					statusCode = httpClient.executeMethod(postMethod); // HttpClient对于要求接受后继服务的请求，象POST和PUT等不能自动处理转发
					System.out.println("statusCode====="+statusCode);
				} catch (Exception e) { 
					  System.out.println("statusCode====="+statusCode);
				      e.printStackTrace();   
			    }
				return String.valueOf(statusCode);
		} 
	    public static void main(String[] args) { 
		       //String url="http://localhost:9090/Test/services/welcom?wsdl";
	    	   String url="http://localhost:8080/Test/services/welcom?wsdl";
	           Map parms=new HashMap(); 
	           HttpClientUtils client=new HttpClientUtils(); 
	           String result=new String(client.getBody(url, parms)); 
	           System.out.println(result);
	    } 
}
